package com.source.lightfowl.barcodeMachineRepair.service;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.source.lightfowl.VO.MachineRepairVO;
import com.source.lightfowl.VO.MachineScrapVO;
import com.source.lightfowl.barcodeMachineRepair.dao.MachineScrapDao;
import com.source.lightfowl.barcodeMachineRepair.pojo.MachineRepair;
import com.source.lightfowl.barcodeMachineRepair.pojo.MachineScrap;
import com.source.lightfowl.person.dao.PersonDao;
import com.source.lightfowl.person.entity.Person;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;


/**
 * 服务层
 *
 * @author Administrator
 *
 */
@Service
public class MachineScrapService {

	@Autowired
	private MachineScrapDao machineScrapDao;
	@Autowired
	private PersonDao personDao;
	/**
	 * 查询全部列表
	 * @return
	 */
	public List<MachineScrapVO> findAll() {
		List<MachineScrap> list = machineScrapDao.findAll();

		List<MachineScrapVO> collect = list.stream().map(s -> {
			MachineScrapVO vo = new MachineScrapVO();
			BeanUtils.copyProperties(s, vo);

//			Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getManagement()).longValue()));
			Person user = personDao.findVO(s.getManagement());
			if(user != null){
				vo.setManagementName(user.getLoginName());
			}

			Person vo1 = personDao.findVO(s.getCreateBy());
			if ( null != vo1){
				vo.setCreateName(vo1.getName());
			}

			return vo;
		}).collect(Collectors.toList());

		return collect;
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public MachineScrapVO findById(BigInteger id) {
		Optional<MachineScrap> optional = machineScrapDao.findById(id);
		if (optional.isPresent()){
			MachineScrap s = optional.get();
			MachineScrapVO vo = new MachineScrapVO();
		    BeanUtils.copyProperties(s, vo);
			Person user = personDao.findVO(s.getManagement());
			if(user != null){
				vo.setManagementName(user.getLoginName());
			}

			Person vo1 = personDao.findVO(s.getCreateBy());
			if ( null != vo1){
				vo.setCreateName(vo1.getName());
			}


		    return vo;
		}

		return null;

	}

	/**
	 * 增加
	 * @param machineScrap
	 */
	public void add(MachineScrap machineScrap) {
		Calendar calendar= Calendar.getInstance();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		String tmp = dateFormat.format(calendar.getTime());
		try{
			Date data = dateFormat.parse(tmp);
			machineScrap.setCreateDate(data);
		}catch (ParseException e){
			System.out.println(e);
		}
		machineScrapDao.save(machineScrap);
	}

	/**
	 * 修改
	 * @param machineScrap
	 */
	public void update(MachineScrap machineScrap) {
		machineScrapDao.save(machineScrap);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(BigInteger id) {
		machineScrapDao.deleteById(id);
	}


	@SuppressWarnings("all")
	public List<MachineScrapVO> findAllPage(int page, int pageNum) {
		Page<MachineScrap> all = machineScrapDao.findAll(PageRequest.of(page - 1, pageNum));

		List<MachineScrapVO> collect = all.get().map(s -> {
			MachineScrapVO vo = new MachineScrapVO();
			BeanUtils.copyProperties(s, vo);

//			Person user = personDao.findVO(BigInteger.valueOf(Long.valueOf(s.getManagement()).longValue()));
			Person user = personDao.findVO(s.getManagement());
			if (user != null) {
				vo.setManagementName(user.getLoginName());
			}

			return vo;

		}).collect(Collectors.toList());


		return collect;
	}
}
